﻿using System;
using System.Windows.Forms;

namespace LinkDownloader.Forms
{
    public partial class frmAddNew : Form
    {

        private static char[] NewLine = new char[] { '\n' };

        private string fileDirectory = string.Empty;
        public string FileDirectory
        {
            get { return this.fileDirectory; }
            set 
            {
                this.lblSaveDirectory.Text = value;
                this.fileDirectory = value;
            }
        }

        private string[] links = null;
        public string[] Links
        {
            get { return this.links; }
        }

        public frmAddNew()
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
        }

        private void GetLinks()
        {
            this.links = this.txtLinks.Text.Split(NewLine, StringSplitOptions.RemoveEmptyEntries);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.GetLinks();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLinks.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLinks.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLinks.Paste();
        }
        
        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLinks.SelectAll();
        }

        private void unselectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLinks.DeselectAll();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLinks.Undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //
        }
        
        private void mnuSaveChange_Click(object sender, System.EventArgs e)
        {
            if (this.ChangeSaveDirectory())
            {
                this.lblSaveDirectory.Text = this.fileDirectory;
            }
        }

        private void lblSaveDirectory_MouseClick(object sender, MouseEventArgs e)
        {
            this.lblSaveDirectory.ContextMenuStrip.Show(this.lblSaveDirectory, e.Location);
        }

        public bool ChangeSaveDirectory()
        {

            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.RootFolder = Environment.SpecialFolder.Desktop;
            dlg.SelectedPath = (this.lblSaveDirectory.Text.Length > 0) ? lblSaveDirectory.Text : this.fileDirectory;
            dlg.ShowNewFolderButton = true;
            dlg.Description = "Select the directory in which to save the file(s).";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.fileDirectory = dlg.SelectedPath;
                return true;
            }
            return false;

        }

    }
}
